package com.ljf.order.service.impl;

import com.ljf.order.dto.OrderDTO;
import com.ljf.order.enums.ResultEnum;
import com.ljf.order.exception.SellException;
import com.ljf.order.service.BuyerService;
import com.ljf.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Ljf
 * @Date : 2019/2/2 18:25
 * @Description :
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private OrderService orderService;

    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
       return checkOrderOwner(openid,orderId);
    }

    @Override
    public OrderDTO cancelOrderOne(String openid, String orderId) {
        OrderDTO dto = checkOrderOwner(openid, orderId);
        if (dto == null) {
            log.error("【取消订单失败】 订单openid 未查到, orderId={}", orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        return orderService.cancel(dto);
    }
    public OrderDTO checkOrderOwner(String openid,String orderId){
        OrderDTO one = orderService.findOne(orderId);
        if (one == null) {
            return null;
        } else if (!one.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("【查询订单】 订单openid 不一致，openid={},orderDto={}", openid, one);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }else{
            return one;
        }
    }
}
